<?php
Yii::app()->theme = Common::getTheme();
class PostController extends Controller
{	
	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{            
            
	}
        
        public function actionNews(){
            $this->pageTitle = "Game bài, Chơi bài online, Đánh bài miễn phí"; 
            Yii::app()->clientScript->registerMetaTag($this->pageTitle, 'title');            
            Yii::app()->clientScript->registerMetaTag("Hệ thống game bài phong phú: tiến lên, sâm lốc, tá lả, xì tố, mậu binh, liêng, chắn, bài cào...Đăng ký miễn phí, tặng 50.000 Xu hàng ngày.", 'description');
            Yii::app()->clientScript->registerMetaTag("game bai, choi bai, danh bai, game bai mien phi, game bai online, tien len, sam loc, ta la, xi to, mau binh, lieng, chan, bai cao", 'keywords');        
            $page = Yii::app()->request->getParam('p');
            if(!isset($page))
                $page = 1;
            $category = PostCategory::model()->find('code_identifier = "tin_tuc"');
            if(!isset($category))
                Common::alert("Không tồn tại danh mục", Yii::app()->homeUrl);
            $countPost =  Post::model()->count('status = 1 AND category_id = '.$category->id);            
            $dataPost = new CActiveDataProvider('Post', array(
                'criteria' => array(
                    'order' => 'create_time DESC',
                    'condition' => 'status = 1 AND category_id = '.$category->id,                        
                ),
                'pagination' => array(
                    'pageSize' => $page * 6,
                ),                    
            ));            
            $this->render('news', array('dataPost'=>$dataPost,'countPost'=>$countPost, 'page'=>$page));
        }
        
         public function actionGuide(){
            $this->pageTitle = "Game bài, Chơi bài online, Đánh bài miễn phí"; 
            Yii::app()->clientScript->registerMetaTag($this->pageTitle, 'title');            
            Yii::app()->clientScript->registerMetaTag("Hệ thống game bài phong phú: tiến lên, sâm lốc, tá lả, xì tố, mậu binh, liêng, chắn, bài cào...Đăng ký miễn phí, tặng 50.000 Xu hàng ngày.", 'description');
            Yii::app()->clientScript->registerMetaTag("game bai, choi bai, danh bai, game bai mien phi, game bai online, tien len, sam loc, ta la, xi to, mau binh, lieng, chan, bai cao", 'keywords');        
            $page = Yii::app()->request->getParam('p');
            if(!isset($page))
                $page = 1;
            $category = PostCategory::model()->find('code_identifier = "huong_dan"');
            if(!isset($category))
                Common::alert("Không tồn tại danh mục", Yii::app()->homeUrl);
            $countPost =  Post::model()->count('status = 1 AND category_id = '.$category->id);            
            $dataPost = new CActiveDataProvider('Post', array(
                'criteria' => array(
                    'order' => 'create_time DESC',
                    'condition' => 'status = 1 AND category_id = '.$category->id,                        
                ),
                'pagination' => array(
                    'pageSize' => $page * 6,
                ),                    
            ));            
            $this->render('news', array('dataPost'=>$dataPost,'countPost'=>$countPost, 'page'=>$page));
        }
        
        public function actionDetail(){            
            $slug = Yii::app()->request->getParam('slug');
            if(isset($slug)){
                $dataPost = Post::model()->find('slug="'.$slug.'"');
                if(!isset($dataPost)){
                    $this->redirect(Yii::app()->createUrl('news'));
                }
            }else{                        
                $id = Yii::app()->request->getParam('id');
                if(!isset($id) || !is_numeric($id))
                    $this->redirect(Yii::app()->createUrl('news'));
                $dataPost = Post::model()->findByPk($id);
                if(!isset($dataPost))
                    $this->redirect(Yii::app()->createUrl('news'));
            }
            $this->pageTitle = strip_tags(trim(html_entity_decode($dataPost->meta_title, ENT_QUOTES, 'UTF-8'), "\xc2\xa0"));
            Yii::app()->clientScript->registerMetaTag(strip_tags(trim(html_entity_decode($dataPost->meta_title, ENT_QUOTES, 'UTF-8'), "\xc2\xa0")), 'title');
            Yii::app()->clientScript->registerMetaTag(strip_tags(trim(html_entity_decode($dataPost->meta_description, ENT_QUOTES, 'UTF-8'), "\xc2\xa0")), 'description');
            Yii::app()->clientScript->registerMetaTag(strip_tags(trim(html_entity_decode($dataPost->meta_keyword, ENT_QUOTES, 'UTF-8'), "\xc2\xa0")), 'keywords');
            Yii::app()->clientScript->registerMetaTag(strip_tags(trim(html_entity_decode($dataPost->meta_title, ENT_QUOTES, 'UTF-8'), "\xc2\xa0")),null,null,array('property'=>'og:title'));
            Yii::app()->clientScript->registerMetaTag((Yii::app()->createAbsoluteUrl($dataPost->main_image)),null,null,array('property'=>'og:image'));
            Yii::app()->clientScript->registerMetaTag((Yii::app()->createAbsoluteUrl($dataPost->sapo)),null,null,array('property'=>'og:description'));
            
            $criteria = new CDbCriteria();
            $criteria->addCondition('id != '.$dataPost->id);
            $criteria->addCondition('category_id ='.$dataPost->category_id);
            $criteria->addCondition('status = 1');
            $criteria->limit = 5;
            $criteria->order = 'create_time DESC';
            $modelPostDiff =  Post::model()->findAll($criteria);
            
            $this->render('detail', array('dataPost'=>$dataPost, 'modelPostDiff'=>$modelPostDiff));
        }
        
        public function actionTags(){
            $keyword = Yii::app()->request->getParam('tag');
            $criteria = new CDbCriteria();            
            $criteria->alias = 'post';            
            $criteria->addCondition('post.status != 0');
            $criteria->addCondition('post.tags like :keyword');
            $criteria->params = array(':keyword'=>'%'.$keyword.'%'); 
            $criteria->group = 'post.id';
                        
            $model = new CActiveDataProvider('Post', array(
                    'criteria' => $criteria,
                    'pagination' => array(
                        'pageSize' => 16,
                    ),                    
                ));            
                      
            $this->render('news', array('dataPost'=>$model)); 
        }

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 */
	public function loadModel()
	{
		if($this->_model===null)
		{
			if(isset($_GET['id']))
			{				
				$this->_model=Post::model()->findByPk($_GET['id']);
			}
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}
	
}
